using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ebookreader.Resources;


namespace ebookreader
{
    public class optionsData
    {
        public int font;
        public int size;
        public bool audio;
    }


    public class OptionMenu : Menu
    {
        //IInputManager input_manager;
        SpriteFont font, font_menu_sx;
        State currentState;

        optionsData current_options;

        public static readonly String[] fonts = new String[] { "Arial", "Orator", "SegoeWP", "TimesNewRoman" };
        public static readonly String[] sizes = new String[] { "14pt", "19pt", "24pt", "29pt" };
        public SpriteFont[] prev_fonts = new SpriteFont[4];

        public OptionMenu(Game game)
            : base(game)
        { }

        public override void Initialize()
        {
            currentState = Game.Services.GetService(typeof(State)) as State;

            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            //Game.Components.Add(new InputManager(Game));
            //input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            input_manager.OnTap += new InputManager.Tap(OnTap);
            input_manager.onBackButtonPressed += new InputManager.Back(() => { ScreenManager.SelectMainMenu(Game); });

            base.Initialize();
        }

        Texture2D back, background, background_er, menu_button,light_back;
        TextureButton next_font, prev_font, next_size, prev_size;
        protected override void LoadContent()
        {
            font = Game.Content.Load<SpriteFont>("Fonts/menu_sx");
            font_menu_sx = Game.Content.Load<SpriteFont>("Fonts/menu");
            background = Game.Content.Load<Texture2D>("Definitivo/background");
            background_er = Game.Content.Load<Texture2D>("todo/logo2");
            back = Game.Content.Load<Texture2D>("Definitivo/back");
            menu_button = Game.Content.Load<Texture2D>("Definitivo/menu_button");
            light_back = Game.Content.Load<Texture2D>("Definitivo/option_sel");
             var right_arrow = Game.Content.Load<Texture2D>("Definitivo/rightArrow2");
             var left_arrow = Game.Content.Load<Texture2D>("Definitivo/leftArrow2");

            int i=0;
             foreach (var f in fonts)
             {
                 prev_fonts[i] = Game.Content.Load<SpriteFont>("Fonts/Usable/"+fonts[i]+"19pt");
                     i++;
             }


             var title_button = new BoxedLabelButton(sprite_batch, menu_button, new Vector2(94, 81), font, Lang.MM_Options, Color.White, false, currentState.options);
            title_button.active = false;
            entries.Add(title_button);

            
            current_options = currentState.options;
            if (current_options == null)
            {
                currentState.options = new optionsData();
                current_options = currentState.options;
            }

            var text_off = font.MeasureString(campione).Y / 2 - light_back.Height / 2;
            next_font = new TextureButton(sprite_batch, right_arrow, new Vector2(582, row1 + text_off), Color.White,
                new Rectangle(572, row1 + (int)text_off - 20, 65, light_back.Height + 20), currentState.options);
            prev_font = new TextureButton(sprite_batch, left_arrow, new Vector2(339, row1 + text_off), Color.White,
               new Rectangle(329, row1 + (int)text_off - 20, 70, light_back.Height + 20), currentState.options);

            next_font.OnPress += new Press(
                (me) =>
                {                    
                    current_options.font = Math.Min(fonts.Length - 1, current_options.font+1);

                });
            entries.Add(next_font);

           
            prev_font.OnPress += new Press(
                (me) =>
                {
                    current_options.font = Math.Max(0, current_options.font - 1);
                   
                });
            entries.Add(prev_font);


            next_size = new TextureButton(sprite_batch, right_arrow, new Vector2(582, row2 + text_off), Color.White,
                new Rectangle(572, row2 + (int)text_off, 65, light_back.Height + 20), currentState.options);
            prev_size = new TextureButton(sprite_batch, left_arrow, new Vector2(339, row2 + text_off), Color.White,
                new Rectangle(329, row2 + (int)text_off, 70, light_back.Height + 20), currentState.options);

            next_size.OnPress += new Press(
                (me) =>
                {
                    current_options.size = Math.Min(sizes.Length - 1, current_options.size + 1);                    
                });
            entries.Add(next_size);


            prev_size.OnPress += new Press(
                (me) =>
                {
                    current_options.size = Math.Max(0, current_options.size - 1);
                });
            entries.Add(prev_size);


            var reset_button = new LabelButton(sprite_batch, font_menu_sx, new Vector2(439, row3), new Color(147, 147, 147), Lang.MM_Reset, currentState.options);
            reset_button.OnPress += new Press(
                (me) =>
                {
                    Game.Components.Add(new AlertPopup(Game, Lang.MM_ResetBookmarks, () => 
                        {
                            foreach(var book in currentState.booksList)
                            {
                                book.bookmarksList.Clear();
                                book.currentWord = 0;
                            }
                            if (currentState.currentBook != null)
                                currentState.currentBook.bookmarksList.Clear();
                            currentState.SaveState();
                        }));
                });
            entries.Add(reset_button);

            var audio_burron = new LabelButton(sprite_batch, font_menu_sx, new Vector2(451, row4), new Color(147, 147, 147), current_options.audio ? Lang.MM_ON : Lang.MM_Off, currentState.options);
            audio_burron.OnPress += new Press(
                (me) => {
                    current_options.audio = !current_options.audio;
                    audio_burron.label = current_options.audio ? Lang.MM_ON : Lang.MM_Off;
                });
            entries.Add(audio_burron);

            var back_button = new TextureButton(sprite_batch, back, Vector2.One * 1, currentState.options);
            back_button.OnPress += new Press(
                (me) => { ScreenManager.SelectMainMenu(Game); }
                );
            entries.Add(back_button);

            base.LoadContent();
        }
               
        protected override void Dispose(bool disposing)
        {
            if(input_manager!=null)
                input_manager.OnTap -= new InputManager.Tap(OnTap);
            //Game.Services.RemoveService(typeof(IInputManager));
            currentState.SaveState();
            base.Dispose(disposing);
        }

        public override void Update(GameTime gameTime)
        {
            next_font.active = current_options.font < fonts.Length - 1;
            next_font.color = current_options.font == fonts.Length - 1 ? Color.White * 0.3f : Color.White * 1f;
            prev_font.active = current_options.font > 0;
            prev_font.color = current_options.font == 0 ? Color.White * 0.3f : Color.White * 1f;
            next_size.active = current_options.size < sizes.Length - 1;
            next_size.color = current_options.size == sizes.Length - 1 ? Color.White * 0.3f : Color.White * 1f;
            prev_size.active = current_options.size > 0;
            prev_size.color = current_options.size == 0 ? Color.White * 0.3f : Color.White * 1f;

            //if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
            //    ScreenManager.SelectMainMenu(Game);
            base.Update(gameTime);
        }


        int row1 = 185, row2 = 255, row3 = 325, row4 = 395;
        string campione = "F";
        int x_center = 477;
        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin();
            sprite_batch.Draw(background, GraphicsDevice.Viewport.Bounds, Color.White);
            sprite_batch.Draw(background_er, new Vector2(664, 23), Color.White);

            //MENU SX allineato a destra a 320

            sprite_batch.DrawString(font, Lang.MM_Font, new Vector2(320 - font.MeasureString(Lang.MM_Font).X, row1), Color.White);
            sprite_batch.DrawString(font, Lang.MM_Dimension, new Vector2(320 - font.MeasureString(Lang.MM_Dimension).X, row2), Color.White);
            sprite_batch.DrawString(font, Lang.MM_ResetState, new Vector2(320 - font.MeasureString(Lang.MM_ResetState).X, row3), Color.White);
            sprite_batch.DrawString(font, Lang.MM_Audio, new Vector2(320 - font.MeasureString(Lang.MM_Audio).X, row4), Color.White);

            var text_off = font.MeasureString(campione).Y/2 - light_back.Height/2;
            sprite_batch.Draw(light_back, new Vector2(380, row1+ text_off), Color.White);
            sprite_batch.Draw(light_back, new Vector2(380, row2 + text_off), Color.White);


            var string_off = font.MeasureString(campione).Y / 2 - prev_fonts[current_options.font].MeasureString(campione).Y / 2;
            sprite_batch.DrawString(prev_fonts[current_options.font], fonts[current_options.font],
                new Vector2(x_center - prev_fonts[current_options.font].MeasureString(fonts[current_options.font]).X / 2, row1 + string_off), Color.White);
            
            string_off = font.MeasureString(campione).Y / 2 - font.MeasureString(campione).Y / 2;
            sprite_batch.DrawString(font, ""+sizes[current_options.size],
                new Vector2(x_center - font.MeasureString("" + sizes[current_options.size]).X / 2, row2 + string_off), Color.White);

            sprite_batch.End();
            base.Draw(gameTime);
        }



    }
}